package com.ml.mall.api.category;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.category.*;
import com.ml.mall.bean.res.category.CategoryBriefRes;
import com.ml.mall.bean.res.category.CategoryRes;
import com.ml.mall.bean.res.category.CategoryTree;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.mapper.category.SysACategoryMapper;
import com.ml.mall.service.category.SysACategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 *   @description    :商品目录相关接口
 *   @author         :zxm
 *   @date           :2021/5/14
 *   @version        :1.0
**/
@Api(tags = "【auth】商品目录相关接口")
@RestController
@RequestMapping("/common/category")
public class SysCategoryController {

    @Autowired
    private SysACategoryService sysACategoryService;

    @ApiOperation(value = "商品目录新增")
    @PostMapping("/new")
//  @RequiresPermissions(PermissionConst.CATEGORY_NEW)
    public ApiRestResult categoryNew(@RequestBody @Valid CategoryNewDto dto){
        return sysACategoryService.createCategory(dto);
    }

    @ApiOperation(value = "商品目录搜索")
    @PostMapping("/search")
//  @RequiresPermissions(PermissionConst.CATEGORY_SEARCH)
    public ApiRestResult categorySearch(@RequestBody  CategorySearchDto dto){
        return sysACategoryService.categorySearch(dto);
    }


    @ApiOperation(value = "商品目录修改")
    @PostMapping("/edit")
//  @RequiresPermissions(PermissionConst.CATEGORY_EDIT)
    public ApiRestResult categoryEdit(@RequestBody @Valid CategoryEditDto dto){
        sysACategoryService.editCategory(dto);
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "商品目录删除")
    @PostMapping("/del")
//  @RequiresPermissions(PermissionConst.CATEGORY_DEL)
    public ApiRestResult categoryRemove(@RequestBody List<Long> ids){
        sysACategoryService.removeCategory(ids);
        return ApiRestResult.ok();
    }
    @ApiOperation(value = "商品目录树形结构")
    @PostMapping("/query_tree")
//  @RequiresPermissions(PermissionConst.CATEGORY_QUERY_TREE)
    public ApiRestResult categoryQueryTree(@RequestBody CategoryDto dto){
        CategoryTree categoryTree = sysACategoryService.queryTreeCategory(dto);
        return ApiRestResult.ok(categoryTree);
    }
    @ApiOperation(value = "商品目录分页查询")
    @PostMapping("/query_page")
//  @RequiresPermissions(PermissionConst.CATEGORY_QUERY_PAGE)
    public ApiRestResult categoryQueryPage(@RequestBody CategoryQueryDto dto){
        Page<CategoryRes> categoryIPage = sysACategoryService.queryPageCategory(dto);
        return ApiRestResult.ok(categoryIPage);
    }
    @ApiOperation(value = "商品目录添加子目录")
    @PostMapping("/add_sub")
//  @RequiresPermissions(PermissionConst.CATEGORY_ADD_SUB)
    public ApiRestResult categorySubAdd(@RequestBody @Valid CategorySubNewDto dto){
        sysACategoryService.addSubCategory(dto);
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "查询对应目录Id的下一级别的目录")
    @PostMapping("/query_level1")
//  @RequiresPermissions(PermissionConst.CATEGORY_QUERY_LEVEL1)
    public ApiRestResult categoryLevel1QueryList(@RequestBody CategoryDto dto){
        List<CategoryBriefRes> categoryBriefResList = sysACategoryService.queryLevel1Category(dto);
        return ApiRestResult.ok(categoryBriefResList);
    }

    @ApiOperation(value = "查询根目录的所有子目录")
    @PostMapping("/query_all_sub")
//  @RequiresPermissions(PermissionConst.CATEGORY_QUERY_ALL_SUB)
    public ApiRestResult categoryLevel1QueryList(@RequestBody CategoryQueryAllSubDto dto){
        return sysACategoryService.queryAllSubByRootCategoryId(dto);
    }
}
